{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39e05873",
   "metadata": {},
   "source": [
    "---------------\n",
    "* 记录人：hj\n",
    "* 时间：21-11-18\n",
    "* week12\n",
    "* 参考文档：\n",
    "> 1.[python官方文档](https://python/org)  \n",
    "> 2.[老师的gitee](https://gitee.com/xzhichao/python)  \n",
    "> 3.[老师的博客](https://www.cnblogs.com/xuzhichao)  \n",
    "---------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bd8ed29",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "## 1.字典的实践（一）\n",
    "* P112-113\n",
    "* 知识点\n",
    "> 1.利用字典计算词频  \n",
    "> 2.字典初始化  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dca0dba2",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "460564c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    "\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "found = found.fromkeys(vowels,0)\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "\n",
    "# print(found.items())\n",
    "for k,v in found.items():\n",
    "    print(k,\"was found\",v,\"time(s)\")\n",
    "# print(found)\n",
    "\n",
    "vowels4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "811876d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一段文本，将统计元音字母出现的次数:chenhai\n",
      "{'a': 1, 'e': 1, 'i': 1, 'o': 0, 'u': 0}\n"
     ]
    }
   ],
   "source": [
    "# 在列表基础上（列表可统计出现的内容有哪些）\n",
    "# 字典可统计出现的内容 + 出现的次数（词频）\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"] # iterable\n",
    "word = input(\"请输入一段文本，将统计元音字母出现的次数:\")\n",
    "found = {}\n",
    "\n",
    "# 利用字典创建的方式，创建字典: dict_name[key]=value\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "\n",
    "# 方法一：循环 vowels\n",
    "# for i in vowels:   # 可优化  fromkeys()\n",
    "#     found[i] = 0\n",
    "# 方法二：利用fromkeys 实现初始化值\n",
    "found = found.fromkeys(vowels,0)\n",
    "    \n",
    "for i in word:#i指的是前面的aeiou\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3be3a6c0",
   "metadata": {},
   "source": [
    "## 字典的高级用法：fromkeys()\n",
    ">1.给定一个序列数据作为key  \n",
    ">2.把序列所有内容进行初始化,作为新的字典内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0f272e6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6642f31e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'alax': 0, 'marry': 0, 'jack': 0, 'jan': 0}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_list=['alax','marry','jack','jan']#填入的内容iterable\n",
    "names_age_dict={}\n",
    "names_age_dict.fromkeys(names_list,0)#把一个序列作为一个字典的keys,然后初始化其值(默认为none),可以给定一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7774dcca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一段文本，将统计元音字母出现的次数:just do it\n",
      "{'a': 0, 'e': 0, 'i': 1, 'o': 1, 'u': 1}\n"
     ]
    }
   ],
   "source": [
    "# 课本字典实践一：\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"] # iterable\n",
    "word = input(\"请输入一段文本，将统计元音字母出现的次数:\")\n",
    "found = {}\n",
    "\n",
    "# 利用字典创建的方式，创建字典: dict_name[key]=value\n",
    "found[\"a\"]=0\n",
    "found[\"e\"]=0\n",
    "found[\"i\"]=0\n",
    "found[\"o\"]=0\n",
    "found[\"u\"]=0\n",
    "\n",
    "found = found.fromkeys(vowels,0)\n",
    "    \n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bdd6185",
   "metadata": {},
   "source": [
    "## 字典的实践（二）p121"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b6107d33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:chenhaijing\n",
      "a 1\n",
      "e 1\n",
      "i 2\n"
     ]
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)  #如果是元音字母，就在字典found中初始化这个元音字母，值为0\n",
    "        found[letter] +=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be71bce8",
   "metadata": {},
   "source": [
    "### 字典的高级用法 setdefault()\n",
    "* 如果字典中还没有该内容（item） ，新增该item作为字典的key，默认值default = None,可以指定值的内容(初始化值的内容)     \n",
    "* 如果 该内容（item） 已经是字典的key， 则不操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "359e6b8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names_age_dict.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "381bc7c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 2, 'e': 0}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {'a':2}\n",
    "found.setdefault('e',0) # e是不存在 found中的,就会直接加在后面\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bf500b7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.setdefault('a',5) # a已经存在在 字典found的中了，则不操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8494e628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 2, 'e': 0}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c2aa3022",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:we can do it\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'e': 1, 'a': 1, 'o': 1, 'i': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 课本字典实践二：\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)  \n",
    "        found[letter] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7d48f7f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 步骤：\n",
    "# h 不在\n",
    "# e found ={'e':1}\n",
    "# l 不在\n",
    "# l 不在\n",
    "# o found ={'e':1,'o':1}\n",
    "#   不在\n",
    "# x 不在\n",
    "# i found ={'e':1,'o':1,'i':1}\n",
    "# a found ={'e':1,'o':1,'i':1,'a':1}\n",
    "# o found = {'e':1,'o':2,'i':1,'a':1}\n",
    "# m 不在\n",
    "# i found = {'e':1,'o':2,'i':2,'a':1}\n",
    "# n 不在\n",
    "# g 不在"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d142b1c",
   "metadata": {},
   "source": [
    "## 课堂练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fc9c10df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Ford': {'Name': 'Ford Prefect',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Researcher',\n",
       "  'Home Planet': 'Betelgeuse Seven'},\n",
       " 'Arthur': {'Name': 'Arthur Dent',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Sandwich-Maker',\n",
       "  'Home Planet': 'Earth'},\n",
       " 'Tricia': {'Name': 'Tricia McMillan',\n",
       "  'Gender': 'Female',\n",
       "  'Occupation': 'Mathematician',\n",
       "  'Home Planet': 'Earth'},\n",
       " 'Marvin': {'Name': 'Marvin',\n",
       "  'Gender': 'Unknown',\n",
       "  'Occupation': 'Paranoid Android',\n",
       "  'Home Planet': 'Unknown'}}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "316ae379",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Tricia McMillan',\n",
       " 'Gender': 'Female',\n",
       " 'Occupation': 'Mathematician',\n",
       " 'Home Planet': 'Earth'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people[\"Tricia\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "feb615bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tricia McMillan'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people[\"Tricia\"][\"Name\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "497f751e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Marvin',\n",
       " 'Gender': 'Unknown',\n",
       " 'Occupation': 'Paranoid Android',\n",
       " 'Home Planet': 'Unknown'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people[\"Marvin\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b3189640",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Unknown'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people[\"Marvin\"][\"Home Planet\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "40ef5429",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Ford', 'Arthur', 'Tricia', 'Marvin'])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#简单的数据处理 查询字典中所有的key\n",
    "people.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0d9dab27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([{'Name': 'Ford Prefect', 'Gender': 'Male', 'Occupation': 'Researcher', 'Home Planet': 'Betelgeuse Seven'}, {'Name': 'Arthur Dent', 'Gender': 'Male', 'Occupation': 'Sandwich-Maker', 'Home Planet': 'Earth'}, {'Name': 'Tricia McMillan', 'Gender': 'Female', 'Occupation': 'Mathematician', 'Home Planet': 'Earth'}, {'Name': 'Marvin', 'Gender': 'Unknown', 'Occupation': 'Paranoid Android', 'Home Planet': 'Unknown'}])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people.values()#查看字典中所有的value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fcb5c3e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "45441290",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40b8c940",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ebb29bb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
